package logic

import (
	"cart_srv/cart"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreategoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreategoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreategoodsLogic {
	return &CreategoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreategoodsLogic) Creategoods(req *types.Creategoodsrequest) (resp *types.Response, err error) {
	goods, err := l.svcCtx.Cartrpc.CreateGoods(l.ctx, &cart.CreateGoodsRequest{
		Title: req.Title,
		Image: req.Image,
		Price: float32(req.Price),
		Num:   int64(req.Num),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "商品发布成功",
		Data:    goods,
	}, nil
}
